package b1.scenes.tower
{
	import b1.conifg.story.*;
	import b1.scenes.BasicScene;

	public class Tower extends BasicScene
	{
		static private var instance:Tower;
		static public const keyNames:Array = ["金色"]; 
		
		private var towerLayers:Object;
		private var towerConfig:TowerConfig;
		public var currentLayer:TowerLayer;
		
		public function Tower()
		{
			super();
		}
		
		static public function getInstance():Tower
		{
			if (!instance)
				instance = new Tower();
			return instance;
		}
		
		public function loadConfig(towerConfig:TowerConfig):void
		{
			clearAllLayers();
			this.towerConfig = towerConfig;
		}
		
		public function loadLayer(layerIndex:int):void
		{
			var towerLayer:TowerLayer = towerLayers[layerIndex];
			if (towerLayer == null)
				towerLayer = new TowerLayer(towerConfig.towerLayerConfigs[layerIndex]);
			currentLayer = towerLayer;
			towerLayers[layerIndex] = towerLayer;
			addChild(towerLayer);
		}
		
		public function removeCurrentLayer():void
		{
			if (currentLayer != null && currentLayer.parent != null)
			{
				currentLayer.parent.removeChild(currentLayer);
				currentLayer = null;
			}
		}
		
		public function clearAllLayers():void
		{
			for (var layerIndex:String in towerLayers)
			{
				var towerLayer:TowerLayer = towerLayers[layerIndex];
				towerLayer.destroy();
				delete towerLayers[layerIndex];
			} 
			towerLayers = new Object();
			removeCurrentLayer();
		}
		
	}
}